package UserInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.jws.Oneway;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.Position;
import javax.swing.text.html.MinimalHTMLWriter;
import javax.xml.bind.Marshaller.Listener;


import Communication.SendSensorManager;
import Setting.SettingData;
import SystemControl.*;
import VirtualMachines.ECU;
import VirtualMachines.MesureDistanceSensor;
import VirtualMachines.ReceiveSensor;
import VirtualMachines.SendSensor;
import VirtualMachines.UIPanel;


public class UI extends JFrame implements ActionListener {

	static Boolean start_flag;
	JPanel p_Sensor;
	JPanel p_UI;
	JPanel p_userInfo;
	JPanel p_userState;
	JPanel p_distance;
	JPanel p_btn;
	JPanel p_rec_data;

	static public JLabel user_name;
	static public JLabel user_state;
	static public JLabel user_distance;
	protected JLabel user_name_text;
	protected JLabel user_state_text;
	protected JLabel user_distance_text;
	protected JLabel label_ECU;
	protected JLabel label_distance;
	protected JLabel label_send;
	protected JLabel label_receive;
	protected JLabel label_line;

	static public JLabel label_ECU_data;
	static public JLabel label_dis_data;
	static public JLabel label_send_data;
	static public JLabel[] label_receive_data;

	static public JLabel[] label_user_list;
	static public JCheckBox[] label_checkbox;

	JButton add_user_btn;
	JButton change_user_btn;
	JButton edit_btn;
	JButton start_btn;

	static Boolean turnOn;
	static String receive_temp;

	static int current_User;

	public static ArrayList<String> temp_list;
	
	
	static public JLabel label_min_distance;
	static public JPanel panel_min_distance;
	JLabel text_min_distance;


	public UI() {
		// TODO Auto-generated constructor stub
		current_User = 1;
		start_flag = false;
		/* ��ü UI �⺻ ���� */
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new GridLayout(3,1));
		setTitle("Intelligent Breaking System");
		setSize(900,500);

		/* Sensor, UI �� ������ sub �г� �� �� layout ���� */
		p_Sensor = new JPanel();
		p_UI = new JPanel();

		p_Sensor.setLayout(new GridLayout(4,2));
		p_UI.setLayout(new GridLayout(4,1));

		/* UI�г� �ؽ�Ʈ ���� */
		user_name_text = new JLabel("User Num. ");
		user_state_text = new JLabel("State");
		user_distance_text = new JLabel("Distance");

		user_name = new JLabel("1");
		user_state = new JLabel("STOP");
		user_distance = new JLabel("0");

		p_userInfo = new JPanel();
		p_userState = new JPanel();
		p_distance = new JPanel();
		p_btn = new JPanel();

		p_userInfo.setLayout(new GridLayout(1,2));
		p_userState.setLayout(new GridLayout(1,2));
		p_distance.setLayout(new GridLayout(1,2));

		p_userInfo.add(user_name_text);
		p_userInfo.add(user_name);

		p_userState.add(user_state_text);
		p_userState.add(user_state);

		p_distance.add(user_distance_text);
		p_distance.add(user_distance);


		p_UI.add(p_userInfo);
		p_UI.add(p_userState);
		p_UI.add(p_distance);

		/* UI ��ư �� �� ������ ��� */
		p_btn.setLayout(new GridLayout(1,4));

		add_user_btn = new JButton("Add User");
		change_user_btn = new JButton("Change User");
		edit_btn = new JButton("Change Setting");
		start_btn = new JButton("Start");

		p_btn.add(add_user_btn);
		p_btn.add(change_user_btn);
		p_btn.add(edit_btn);
		p_btn.add(start_btn);

		p_UI.add(p_btn);

		add_user_btn.addActionListener(this);
		change_user_btn.addActionListener(this);
		edit_btn.addActionListener(this);
		start_btn.addActionListener(this);

		/* Sensor �г� �� �� ���� */
		label_ECU = new JLabel("ECU");
		label_distance = new JLabel("Distance");
		label_send = new JLabel("Send");
		label_receive = new JLabel("Receive");
		label_ECU_data = new JLabel("1");
		label_dis_data = new JLabel("");
		label_send_data = new JLabel("2");
		label_receive_data = new JLabel[3];
		p_rec_data = new JPanel();

		p_rec_data.setLayout(new GridLayout(3,1));

		for(int i = 0; i < 3; i++) {
			label_receive_data[i] = new JLabel("");
		}

		p_rec_data.add(label_receive_data[0]);
		p_rec_data.add(label_receive_data[1]);
		p_rec_data.add(label_receive_data[2]);

		p_Sensor.add(label_ECU);
		p_Sensor.add(label_ECU_data);
		p_Sensor.add(label_distance);
		p_Sensor.add(label_dis_data);
		p_Sensor.add(label_send_data);
		p_Sensor.add(label_send);
		p_Sensor.add(label_send_data);
		p_Sensor.add(label_receive);
		p_Sensor.add(p_rec_data);


		/* �� Sensor, UI �г� ��� */
		label_line = new JLabel("---------------------------------" +
				"---------------------------------------------" +
				"--------------------------------------------");

		add(p_Sensor);
		//add(label_line);
		add(p_UI);

		p_Sensor.setBorder(new TitledBorder(new EtchedBorder(), "Sensor"));
		p_UI.setBorder(new TitledBorder(new EtchedBorder(), "User Interface"));

		turnOn = true;

		setVisible(true);

		setReceive();
		
		user_state.setOpaque(true);
		
		panel_min_distance = new JPanel();
		label_min_distance = new JLabel("min");
		text_min_distance = new JLabel("min distance");
		
		panel_min_distance.setLayout(new GridLayout(1,2));
		panel_min_distance.add(text_min_distance);
		panel_min_distance.add(label_min_distance);
		
		panel_min_distance.setBorder(new TitledBorder("Distance Information"));
		add(panel_min_distance);
		

		SettingData.current_user = 1;
		current_User = 1;
		user_name.setText("1");
	}

	public void setUserName(int input) {
		user_name.setText(""+input);
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		final JFrame add_Panel = new JFrame();
		if(add_user_btn == e.getSource()) {
			JOptionPane.showMessageDialog(this, "Create Account\n" + "ID : "+(SettingData.data.size()+1));
			try {
				SystemManager.add_user();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		else if(e.getSource() == change_user_btn) {
			System.out.println(""+SettingData.data.size() + " " + SettingData.current_user);
			if(SettingData.data.size() != SettingData.current_user) {
				SettingData.current_user++;
				current_User++;
				user_name.setText(""+current_User);

			}
			else {
				SettingData.current_user = 1;
				current_User = 1;
				user_name.setText("1");
			}
		}
		else if(e.getSource() == edit_btn) {
			JLabel text_distance = new JLabel("input distance rate : ");
			JLabel text_sound = new JLabel("input sound rate : ");
			final JTextField input_distance_rate = new JTextField(5);
			final JTextField input_sound_rate = new JTextField(5);
			JButton btn_ok = new JButton("OK");
			JButton btn_cancel = new JButton("Cancel");

			add_Panel.setSize(300, 400);

			add_Panel.setLayout(new GridLayout(3,2));

			add_Panel.add(text_distance);
			add_Panel.add(input_distance_rate);
			add_Panel.add(text_sound);
			add_Panel.add(input_sound_rate);
			add_Panel.add(btn_ok);
			add_Panel.add(btn_cancel);

			btn_ok.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					try {
						if(Integer.parseInt(input_distance_rate.getText()) < 100 && Integer.parseInt(input_sound_rate.getText()) < 100) {
							JOptionPane.showMessageDialog(null, "Input number is positive integer");
						}
						else 
							SystemManager.set_distance_rate(current_User-1, input_distance_rate.getText(), input_sound_rate.getText());
					} catch (Exception er) {
						JOptionPane.showMessageDialog(null, "Input number is integer");
					}
					add_Panel.show(false);
				}
			});

			add_Panel.show(true);

			btn_cancel.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					add_Panel.show(false);
				}
			});

			add_Panel.show(true);
		}
		else if(e.getSource() == start_btn) {
			if(!start_flag) {
				start_flag = true;
				start_btn.setText("Stop");
				SystemManager.compute_risk();
			}
			else {
				start_flag = false;
				start_btn.setText("Start");
				SystemManager.stop_compute();
			}
		}
	}

	public static void setReceive() {
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(2000);
					while(turnOn){
						temp_list = new ArrayList<String>();
						try {
							temp_list = (ArrayList<String>) ReceiveSensor.showReceiveSensorData();
							for(int i=0; i < ReceiveSensor.showReceiveSensorData().size(); i++) {
								label_receive_data[i].setText(temp_list.get(i));
							}
						} catch(Exception e) {
							label_receive_data[0].setText("loading");
						}
						label_dis_data.setText(MesureDistanceSensor.showDistanceSensorData());
						label_ECU_data.setText(ECU.showECUData());
						label_send_data.setText(SendSensor.showSendSensor());
						user_state.setText(UIPanel.user_state);
						user_distance.setText("" + UIPanel.distance);
						Thread.sleep(10);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}).start();
	}

}

